
(function(){
	function i(){
	var n = this;
	this.pageSize = 15;
	this.inti = function(){
			$("#search-input").on('keydown',function(k) {
                k.which == 13 && (window.location.href = "/product?keyword=" + $("#search-input").val())
            });
			$("#btn-search").click(function() {
                window.location.href = "/product?keyword=" + $("#search-input").val()
            });
			$(window).on('scroll',n.lazyload).trigger("scroll");
		}
	this.searchProduct = function(p){
			$.post("/product/ajax",p,function(msg){
				msg.status == 1 ? n.loaded(JSON.parse(msg.data)):$('#wrapper').html('<div class="dataEmpty">未找到机型</div>');
			});	
		}
	this.lazyload = function(){
		if ($("body").scrollTop() + $(window).height() > $("body").height() - 100) {
			$(window).off("scroll", n.lazyLoad);
			$("#wrapper").append('<div id="loading">加载中...<\/div>');			
			var b = $("#wrapper").data('bid');
			var u = $("#wrapper").data('keyword');
			var p =  Math.ceil(($("#wrapper li").length - 1) / n.pageSize) + 1;
			n.searchProduct({
				brand:b,keyword:u,page:p
				});				  
		}
	}
	this.loaded = function (t){
		t.length >= n.pageSize && $(window).on('scroll',n.lazyload);	
		$("#loading").remove();
		$.each(t,function(i,e){			
			li = document.createElement('li'); 	
			li.innerHTML = "<a href=\"choix/?id="+e.id+"\">"+e.pname+"<span>￥ "+e.price+"</span></a>";
			$('#wrapper ul').append(li);								
		});		
	}
}
	var f = new i();
	f.inti();
	})();	
